<template>
	<view>
		<view class="shoptop flex_jc_sb">
			<image src="/static/cartop.png" mode=""></image>
			<view class="btn" @click="changeType">
				{{istype?'管理':'完成'}}
			</view>
		</view>
		<view class="ulist">
			<view class="item " v-for="(item,index) in shopList" :key="index" @click.stop="changSelect(item)">
				<view class="yuan">
					<image v-show="!item.isclick" src="/static/radio.png" mode=""></image>
					<image v-show="item.isclick" src="/static/radioact.png" mode=""></image>
				</view>
				<view class=" flex_r">
					<image :src="item.product.icon" mode="aspectFit"></image>
					<view class="item_r">
						<view class="title">{{item.product.name}}</view>
						<view class="item_flex">
							<view class="price">
								<text>￥</text>{{item.product.price}}<text class="t2">/{{item.product.unit}}</text>
							</view>

							<view class="inputflex" @click.stop="">
								<uni-icons @click.stop="jian(item)" v-show="item.productCount>0" type="minus" size="26"
									color="#FF7700"></uni-icons>
								<input v-show="item.productCount>0" @blur='inputblur($event,item)'
									:value="item.productCount" type="number" @input="handleInput($event,item)"
									confirm-type="done" />
								<uni-icons type="plus-filled" size="26" color="#FF7700"
									@click.stop="add(item)"></uni-icons>
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
		<view class="k1" v-if="shopList.length==0">
			<image src="/static/gkong.png" mode=""></image>
			<view class="texts">
				购物车空空如也~~
			</view>
		</view>
		<view class="" style="height: 200rpx;">
		</view>
		<view class="btn_bottom flex_jc_sb">
			<view class="btn_left flex" @click="changeAll">
				<image v-show="isAll" src="/static/radioact.png" mode=""></image>
				<image v-show="!isAll" src="/static/radio.png" mode=""></image>
				<view class="">
					全选
				</view>
			</view>
			<view class="btn_right dele" v-show="!istype" @click="removeAllShop">
				删除
			</view>
			<view class="btn_right flex" v-show="istype">
				<view class="btnprice">
					合计: <text class="t1">￥</text> <text class="t2">{{allPrice}}</text>
				</view>
				<view class="btn" @click="pageGo">
					结算({{allCount}})
				</view>
			</view>
		</view>
		<uv-tabbar :value="2" class="tab">
		</uv-tabbar>
	</view>
</template>

<script setup>
	import {
		ref,
		watch,
		computed
	} from 'vue';
	import {
		onLoad,
		onShow,
		onReachBottom
	} from '@dcloudio/uni-app'
	import {
		useStore
	} from 'vuex';
	const store = useStore();
	import {
		getShopCar,
		setShopCarCount,
		removeShopCar
	} from '@/common/http.api.js'

	const shopList = ref([])
	onShow(() => {
		getCar()
	})

	const allPrice = computed(() => {
		let price = 0;
		shopList.value.forEach(its => {
			if (its.isclick) {
				price += its.product.price * its.productCount
			}
		})
		return price.toFixed(2)
	})
	const allCount = computed(() => {
		let total = 0;
		shopList.value.forEach(its => {
			if (its.isclick) {
				total += its.productCount
			}
		})
		return total
	})
	const isAll = computed(() => {
		
		const isActve = shopList.value.every((item) => {
			return item.isclick
		})
		if(shopList.value.length!=0){
			return isActve
		}else{
			return false
		}
	})
	const istype = ref(true)

	function changeType() {
		istype.value = !istype.value
	}
	// 	全选
	function changeAll() {
		const isActve = shopList.value.every((item) => {
			return item.isclick
		})
		if (isActve) {
			shopList.value.forEach(its => {
				its.isclick = false
			})
		} else {
			shopList.value.forEach(its => {
				its.isclick = true
			})
		}
	}
	// 点击切换
	function changSelect(item) {
		item.isclick = !item.isclick
	}
	function removeShop(ids){
		removeShopCar({ids}).then(res => {
			if (res.code == 200) {
				let total = res.data.reduce((sum, e) => sum + Number(e.productCount ||
					0), 0)
				store.commit('setShopNum', total)
				getCar()
			}
		})
	}
	//删除所有
	function removeAllShop() {
		if(shopList.value.some(item => item.isclick)){
			uni.showModal({
				title: '',
				content: '确定要把该商品移除购物车嘛？！',
				confirmText: '确定',
				confirmColor: '#FF7700',
				success: (res) => {
					if (res.confirm) {
						let ids = shopList.value.filter(item=>{return item.isclick}).map(item=>{return item.product.id})
						removeShop(ids)
					} else if (res.cancel) {
					}
				}
			});
		}else{
			uni.showToast({
				title:'请选择商品',
				icon:'none'
			})
		}
	}

	function pageGo() {
		if(shopList.value.some(item => item.isclick)){
			let list = shopList.value.filter(item=>{return item.isclick})
			uni.setStorageSync('shopList',list)
			uni.navigateTo({
				url: '/pages/shopcart/createOrder'
			})
		}else{
			uni.showToast({
				title:'请选择商品',
				icon:'none'
			})
		}
	}

	function getCar() {
		getShopCar({}).then(res => {
			res.data.forEach(item => {
				item.isclick = false
			})
			shopList.value = res.data
		})
	}
	const handleInput = (event, item) => {
		item.productCount = event.target.value
	};

	function add(item) {
		item.productCount++
		setCount(item.product.id, item.productCount)
	}

	function jian(item) {
		item.productCount--
		if (item.productCount == 1 || item.productCount == 0) {
			uni.showModal({
				title: '',
				content: '确定要把该商品移除购物车嘛？！',
				confirmText: '确定',
				confirmColor: '#FF7700',
				success: (res) => {
					if (res.confirm) {
						removeShop([item.product.id])
					} else if (res.cancel) {
						item.productCount = 1
					}
				}
			});
		}
	}

	function inputblur(e, item) {
		if (e.detail.value < 1) {
			uni.showToast({
				title: '最小值为1',
				icon: 'none'
			})
			item.productCount = 1
			// setCount(item.product.id, e.detail.value)
		} else {
			setCount(item.product.id, item.productCount)
		}
	}

	function setCount(id, num) {
		setShopCarCount({
			productId: id,
			productCount: num
		}).then(res => {
			if (res.code == 200) {
				let total = res.data.reduce((sum, e) => sum + Number(e.productCount || 0), 0)
				store.commit('setShopNum', total)
			}
		})
	}
</script>

<style lang="scss">
	page {
		background: #F8F8F8;
	}

	.k1 {
		text-align: center;
		margin: 200rpx auto;
		width: 320rpx;

		image {
			width: 320rpx;
			height: 300rpx;
		}

		.texts {
			font-family: PingFang SC, PingFang SC;
			font-weight: 500;
			font-size: 28rpx;
			color: #979797;
			margin-top: 10rpx;
		}
	}

	.shoptop {
		width: 750rpx;
		height: 100rpx;
		background: #FFFFFF;
		border-radius: 0rpx 0rpx 0rpx 0rpx;
		padding: 0rpx 20rpx;
		box-sizing: border-box;
		position: sticky;
		top: 0%;
		z-index: 9;

		image {
			width: 300rpx;
			height: 32rpx;
		}

		.btn {
			width: 100rpx;
			height: 48rpx;
			background: #FFFFFF;
			border-radius: 30rpx 30rpx 30rpx 30rpx;
			border: 1px solid #AEAEAE;
			font-family: PingFang SC, PingFang SC;
			font-weight: 500;
			font-size: 28rpx;
			color: #000000;
			line-height: 48rpx;
			text-align: center;
		}
	}

	.ulist {
		width: 710rpx;
		margin: 20rpx auto;

		.item {
			width: 710rpx;
			background: #FFFFFF;
			padding: 20rpx;
			box-sizing: border-box;
			display: flex;
			border-bottom: 1px solid #efefef;
			align-items: center;

			.yuan {
				image {
					width: 40rpx;
					height: 40rpx;
				}
			}

			.flex_r {
				display: flex;
				margin-left: 30rpx;
				flex: 1;

				image {
					width: 120rpx;
					height: 120rpx;
					border-radius: 20rpx;
					border: 1px solid #efefef;
				}

				.item_r {
					flex: 1;
					margin-left: 20rpx;
					display: flex;
					flex-direction: column;
					justify-content: space-between;

					.title {
						font-family: PingFang SC, PingFang SC;
						font-weight: bold;
						font-size: 30rpx;
						color: #000000;
					}

					.item_flex {
						display: flex;
						justify-content: space-between;
						align-items: center;
					}
				}
			}

		}

		.item:last-child {
			border: none;
		}
	}

	.btn_bottom {
		width: 100%;
		height: 100rpx;
		z-index: 9;
		bottom: 0%;
		position: fixed;
		margin-bottom: 100rpx;
		padding-bottom: constant(safe-area-inset-bottom);
		padding-bottom: env(safe-area-inset-bottom);
		line-height: 100rpx;
		background: #fff;

		.btn_left {
			margin-left: 20rpx;
			font-family: PingFang SC, PingFang SC;
			font-weight: 500;
			font-size: 28rpx;
			color: #000000;

			image {
				width: 40rpx;
				height: 40rpx;
				margin-right: 20rpx;
			}
		}

		.dele {
			width: 200rpx;
			height: 80rpx;
			background: #FF0000;
			border-radius: 40rpx 40rpx 40rpx 40rpx;
			font-family: PingFang SC, PingFang SC;
			font-weight: 500;
			font-size: 28rpx;
			color: #FFFFFF;
			line-height: 80rpx;
			text-align: center;
		}

		.btn_right {
			margin-right: 20rpx;


			.btnprice {
				font-family: PingFang SC, PingFang SC;
				font-weight: 500;
				font-size: 28rpx;
				color: #000000;

				.t1 {
					color: #FF0000;
				}

				.t2 {
					font-size: 40rpx;
					color: #FF0000;
				}
			}

			.btn {
				margin-left: 30rpx;
				width: 200rpx;
				height: 80rpx;
				background: #FF7700;
				border-radius: 40rpx 40rpx 40rpx 40rpx;
				font-family: PingFang SC, PingFang SC;
				font-weight: 500;
				font-size: 28rpx;
				color: #FFFFFF;
				line-height: 80rpx;
				text-align: center;
			}
		}
	}

	.inputflex {
		display: flex;
		height: 46rpx;
		text-align: center;
		align-items: center;

		input {
			font-family: PingFang SC, PingFang SC;
			font-weight: bold;
			font-size: 30rpx;
			color: #FF0000;
			max-width: 60rpx;
			line-height: 46rpx;
		}
	}
</style>